package com.example.kwokg.meishi.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.kwokg.meishi.R;
import com.example.kwokg.meishi.user.User;

import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by kwokg on 2016/9/18.
 */
public class Register extends AppCompatActivity {

    private EditText edt_username,edt_password,edt_vfcode;
    private String username,password,vfcode;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        setTitle("注册");


        initView();

    }

    //初始化
    private void initView() {
        edt_username = (EditText) findViewById(R.id.register_username);
        edt_password = (EditText) findViewById(R.id.register_password);
        edt_vfcode = (EditText) findViewById(R.id.vfcode);
    }

    //判断手机号和密码格式
    private boolean isTextValid(){
        getEditText();
        return !(username.length()!=11||password.length()<6||password.length()>16);
    }

    //获取编辑框信息
    private void getEditText() {
        username = edt_username.getText().toString();
        password = edt_password.getText().toString();
        vfcode = edt_vfcode.getText().toString();
    }

    //获取验证码
    public void getVfcode(View view) {
        edt_vfcode.setText("");
        //输入是否合法
        if (!isTextValid()){
            Toast.makeText(Register.this, "请检查手机号码和密码是否正确！", Toast.LENGTH_SHORT).show();
            return;
        }
        //发送验证码
        BmobSMS.requestSMSCode(username, "手机号码登陆模板", new QueryListener<Integer>() {
                @Override
                public void done(Integer integer, BmobException e) {
                    if (e==null){
                        Toast.makeText(Register.this, "发送成功！请确认验证码", Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(Register.this, "验证码发送失败！请检查手机号是否正确", Toast.LENGTH_SHORT).show();
                    }
                }
            });
    }

    //用户注册
    public void userRegister(View view) {
        //输入判断
        if (!isTextValid()||vfcode.length()!=6){
            Toast.makeText(Register.this, "请检查输入项是否有误", Toast.LENGTH_SHORT).show();
            return;
        }
        BmobSMS.verifySmsCode(username, vfcode, new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e!=null){
                    Toast.makeText(Register.this, "验证码错误！", Toast.LENGTH_SHORT).show();
                }else{
                    //验证成功
                    BmobUser user=new BmobUser();
                    user.setUsername(username);
                    user.setPassword(password);
                    //保存到数据库
                    user.signUp(new SaveListener<User>() {
                        @Override
                        public void done(User o, BmobException e) {
                            if (e==null){
                                Toast.makeText(Register.this, "注册成功！", Toast.LENGTH_SHORT).show();
                                //登陆成功后跳转
                                startActivity(new Intent(Register.this,MainActivity.class).putExtra("name",username));
                                overridePendingTransition(0,0);
                                finish();
                            }else{
                                Log.e("Tag", "done: e",e);
                            }
                        }
                    });
                }
            }
        });

    }

    @Override
    protected void onPause() {
        super.onPause();
        overridePendingTransition(0,0);
    }
}
